// pages/address/address.js

Page({

    /**
     * 页面的初始数据
     */
    data: {
        username: "",
        phone: "",
        address: ""
    },

    /**
     * 生命周期函数--监听页面加载
     http://localhost:8080/web/user/v1/findByName
     */
    onLoad: function (options) {
        var that = this;
        wx.getStorage({
            key: 'username',
            success(res) {
                that.setData({
                    username: res.data
                })
            }
        })
        var username = JSON.stringify(that.data.username)
        wx.request({
            url: 'http://localhost:8080/web/user/v1/findByName?username=用户or1tz5WAg7F',
            method: "GET",
            data: {},
            header: {
                "Content-Type": "application/json;charset=UTF-8"
            },
            success: function (res) {
                console.log(res);
                if (res.data.code == 200) {
                    that.setData({
                        username: res.data.data.username,
                        phone: res.data.data.phone,
                        address: res.data.data.address
                    })
                }

            },
            fail: function (err) {
                //失败执行
            }
        })
    },
    // 返回按钮功能
    onfalse: function () {
        wx.switchTab({
            url: '../my/my'
        })
    },
    //修改按钮功能
    onsure: function () {
        var that = this
        console.log(that.data.username)
        console.log(that.data.phone)
        console.log(that.data.address)
        wx.request({
            url: 'http://localhost:8080/web/user/v1/update?username=' + that.data.username + '&phone=' + that.data.phone + '&address=' + that.data.address,
            data: {
                // usersname: "zhangsan",
                // phone: "2222",
                // address: "重庆"
            },
            method: 'POST',
            header: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
                console.log(res)
                if (res.data.state == 1) {
                    that.setData({
                        title: res.data.data
                    })
                } else {
                    wx.showToast({
                        title: res.data.msg
                    });
                }
            }
        })
    },
    usernameInput: function (e) {
        this.data.username = e.detail;
    },
    phoneInput: function (e) {
        this.data.phone = e.detail;
    },
    onInput: function (e) {
        this.data.address = e.detail;
    }
})